	cd "C:\Users\jbube\Documents\Dropbox\WhySatesIntervene\data\repdata"
	
*	Replication Data for:

*	Why Do States Intervene in the Elections of Others?The Role of Incumbent-Opposition Divisions
*	Johannes Bubeck, Kai Jäger, Nikolay Marinov, Federico Nanni
*	March 2020

	set scheme s1color

*	STATA
*	install packages:  grc1leg 

*	we start w Dennis version of data, remember it does not have all dyads:
*	files: 

*	ab_rile.dta
*	alarm.dta
*	book_data_idealpoints_merged.csv
*	c_sample.dta
*	hr_sanct_yr.dta
*	dev.dta
*	gov_opp_predicted.dta
*	icr.dta
*	id & q-wide.dta
*	replication_data_final.dta
*   region.dta -> for jb file

*	this merge happens last and is "temporary", then data is dropped
*	merge 1:1 electionid using ab_rile.dta

	drop _all

	import delimited rawdata_notinclude/book_data_idealpoints_merged.csv, clear 

    * for some reason intervener ccode is string

	destring i_ccode, replace force

	* Here we merge w Book Rules rep archive:

	sort electionid i_ccode

	merge 1:1 electionid i_ccode using rawdata_notinclude/replication_data_final.dta, force

	drop _merge

	sort electionid i_ccode

	* this is DH measure of political alignment from Voeten?

	destring absidealdiff, gen(undiff) force

	gen unusdiff=undiff if i_ccode==2

	* rescale on 0 1 then on -1 to 1

	gen unusalign=(5-unusd)/5

	replace unusalign=2*(unusalign-.5)

	* derive new label for groups

	gen cy=substr(country,1,8)+" "+substr(electionid,5,4)

	* fix cases

	replace cy="Serbia 2002" if cy=="Serbia ( 2002"
	
	* Here we merge w/ cites in Congress

	merge m:1 ccode i_ccode using rawdata_notinclude/c_sample.dta

	replace sample=sample2 if sample==. & sample2!=.

	drop if _m==2

	drop _m

	
	* Here we merge w cites in Congress - so for US only

	merge m:1 ccode year using rawdata_notinclude/hr_sanct_yr.dta

	drop if _m==2

	drop _m


	* Here we merge w Al ARM and NELDA (remember some ids do not merge from nelda) - remeber some things such as issue are for US only as int, AL ign ARM s and END orsement

	merge m:1 electionid using rawdata_notinclude/alarm.dta

	drop if _m==2

	drop _m


	* Here we merge w ICR - remember this only for US!

	merge m:1 electionid using rawdata_notinclude/icr

	drop if _m==2

	drop _m


	* Here we merge w Developed yes or no

	merge m:1 ccode year using rawdata_notinclude/dev

	drop if _m==2

	drop _m

	
	* Here we merge w predicted class by FN

	* remember to rename the a gov to pred 

	merge m:1 electionid intervener using rawdata_notinclude/gov_opp_predicted.dta

	drop if _m==2

	drop _m
	
	drop pol
		
	* NELDA

	merge m:1 electionid using "rawdata_notinclude/id & q-wide.dta", keepusing(nelda20 nelda31 nelda33)

	drop if _m==2

	drop _m

	gen contested=(nelda20=="yes") if nelda20!=""
	
	preserve
	use rawdata_notinclude/region.dta, clear
	destring cowcode, generate(ccode) ignore("NA")
	tempfile reg_file
	duplicates drop ccode, force
	save `reg_file', replace
	restore
	
	merge m:1 ccode using `reg_file'
	
	drop if _m==2

	drop _m
	
	encode sftgreg, gen(region_code)
	
	****************************************************************************
	****************************************************************************	
	** Merge CMP data
	****************************************************************************	
	****************************************************************************

	merge m:1 electionid using rawdata_notinclude/ab_rile.dta
	
	replace a_rile = . if i_ccode!=2
	replace b_rile = . if i_ccode!=2
	
	rename _merge CMP_merge

	* drop large vars and strange vars
	drop *0 *_zero *_plus *_min v1 v3*
	
	save replication_data.dta, replace
		
	
	*	Replication Data for:

	*	Why Do States Intervene in the Elections of Others?The Role of Incumbent-Opposition Divisions
	*	Johannes Bubeck, Kai Jäger, Nikolay Marinov, Federico Nanni
	*	March 2020
	
	global path "C:\Users\jbube\Documents\Dropbox\WhySatesIntervene\data\repdata"
	cd $path
	
	use replication_data.dta, clear
	
	//make dir for all output
	cap mkdir replication_output
	
	****************************************************************************
	****************************************************************************
	** Prelims for ALL COUNTRIES
	****************************************************************************
	****************************************************************************

	* Process and Candidate Interventions by All Potential (Sample) Interveners; Interventions with Machine-Predicted Polarization (All Interveners
		
	* version pol ignoring all that are outside of -1  to 1	
	
	gen pol=a_gov-b_opp if abs(a_gov)<=1 & abs(b_opp)<=1
	
	gen polpred=a_gov_pred-b_opp_pred
	
	* This is for all interventions:

	gen p2=p+2 if abs(p)<=1
	
	////////////////////////////////////////////////////////////////////////////	
	// Table 5
	////////////////////////////////////////////////////////////////////////////
	
	* Higher obs count due to better merging
	
	regress c pol if abs(pol)<=2 & abs(c)<=1 & sample==1, vce(cluster ccode)
	outreg2 using replication_output/table_5_cfile_all, tex replace
	regress c pol if abs(pol)<=2 & abs(c)<=1 & sample==1 & year<1990, vce(cluster ccode)
	outreg2 using replication_output/table_5_cfile_all, tex append
	regress c pol if abs(pol)<=2 & abs(c)<=1 & sample==1 & year>=1990 & year!=., vce(cluster ccode)
	outreg2 using replication_output/table_5_cfile_all, tex append

	regress p pol if abs(pol)<=2 & abs(p)<=1 & sample==1, vce(cluster ccode)
	outreg2 using replication_output/table_5_pfile_all, tex replace
	regress p pol if abs(pol)<=2 & abs(p)<=1 & sample==1 & year<1990, vce(cluster ccode)
	outreg2 using replication_output/table_5_pfile_all, tex append
	regress p pol if abs(pol)<=2 & abs(p)<=1 & sample==1 & year>=1990 & year!=., vce(cluster ccode)
	outreg2 using replication_output/table_5_pfile_all, tex append
	
	////////////////////////////////////////////////////////////////////////////
	// Figure 3
	////////////////////////////////////////////////////////////////////////////
	
	* Predicting CI for predict w diff vars, consider, combining into one three-way
	
	twoway (lfitci c pol if abs(pol)<=2 & abs(c)<=1 & sample==1, ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitpp_all, replace))  || (lfitci p2 pol if abs(pol)<=2 & abs(p)<=1 & sample==1, ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))
	twoway (lfitci c pol if abs(pol)<=2 & abs(c)<=1 & sample==1 & year<1990 & year!=., ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitpp_allpre, replace))  || (lfitci p2 pol if abs(pol)<=2 & abs(p)<=1 & sample==1 & year<1990 & year!=., ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))
	twoway (lfitci c pol if abs(pol)<=2 & abs(c)<=1 & sample==1 & year>=1990 & year!=., ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitpp_allpost, replace))  || (lfitci p2 pol if abs(pol)<=2 & abs(p)<=1 & sample==1 & year>=1990 & year!=., ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))

	gr combine lfitpp_all lfitpp_allpre lfitpp_allpost, col(3) iscale(1) graphregion(color(white))

	graph export replication_output/fig_3_lfitcomb_all.pdf, replace
	
	////////////////////////////////////////////////////////////////////////////
	// Figure F.6
	////////////////////////////////////////////////////////////////////////////
	
	* This  is same  but w predicted a and b classes

	twoway (lfitci c polpred if abs(c)<=1 & sample==1, ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitpp_all_pred, replace))  || (lfitci p2 polpred if abs(p)<=1 & sample==1, ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))
	twoway (lfitci c polpred if abs(c)<=1 & sample==1 & year<1990 & year!=., ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitpp_allpre_pred, replace))  || (lfitci p2 polpred if abs(p)<=1 & sample==1 & year<1990 & year!=., ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))
	twoway (lfitci c polpred if abs(pol)<=2 & abs(c)<=1 & sample==1 & year>=1990 & year!=., ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitpp_allpost_pred, replace))  || (lfitci p2 polpred if abs(p)<=1 & sample==1 & year>=1990 & year!=., ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))

	gr combine lfitpp_all_pred lfitpp_allpre_pred lfitpp_allpost_pred, col(3) iscale(1) graphregion(color(white))

	graph export replication_output/fig_F_6_lfitcomb_all_pred.pdf, replace	

	////////////////////////////////////////////////////////////////////////////
	//Figure F7
	////////////////////////////////////////////////////////////////////////////

	* Elections in which Incumbent Office Contested/Not contested (All Interveners)

	* NELDA20 elections in which inc competes	
	
	twoway (lfitci c pol if abs(c)<=1 & sample==1, ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitpp_all, replace))  || (lfitci p2 pol if abs(p)<=1 & sample==1, ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))
	twoway (lfitci c pol if abs(c)<=1 & sample==1 & contested==1, ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitpp_all_cont1, replace))  || (lfitci p2 pol if abs(p)<=1 & sample==1 & contested==1, ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))
	twoway (lfitci c pol if abs(pol)<=2 & abs(c)<=1 & sample==1 & contested==0, ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitpp_all_cont0, replace))  || (lfitci p2 pol if abs(p)<=1 & sample==1 & contested==0, ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))

	gr combine lfitpp_all lfitpp_all_cont1 lfitpp_all_cont0, col(3) iscale(1) graphregion(color(white))
	
	graph export replication_output/fig_F_7_lfitcomb_all_cont10.pdf, replace

	
	****************************************************************************
	****************************************************************************	
	** ONLY US OF INTEREST
	****************************************************************************	
	****************************************************************************
	
	keep if i_ccode==2

	////////////////////////////////////////////////////////////////////////////
	//Figure E.4
	////////////////////////////////////////////////////////////////////////////

	* US Process and Candidate Interventions – Issues at Stake

	* This is for Issue Hi or Lo interventions:

	twoway (lfitci c pol if abs(pol)<=2 & abs(c)<=1, ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitpp, replace))  || (lfitci p2 pol if abs(pol)<=2 & abs(p)<=1, ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))

	twoway (lfitci c pol if abs(pol)<=2 & abs(c)<=1 & alarmhi==0, ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitpp_alarmlo, replace))  || (lfitci p2 pol if abs(pol)<=2 & abs(p)<=1 & alarmhi==0, ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))

	twoway (lfitci c pol if abs(pol)<=2 & abs(c)<=1 & alarmhi==1, ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitpp_alarmhi, replace))  || (lfitci p2 pol if abs(pol)<=2 & abs(p)<=1 & alarmhi==1, ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))
	
	gr combine lfitpp lfitpp_alarmlo lfitpp_alarmhi, col(3) iscale(1) graphregion(color(white))

	graph export replication_output/fig_E_4_lfitcomb_alarm.pdf, replace

	////////////////////////////////////////////////////////////////////////////
	// Table E.7
	////////////////////////////////////////////////////////////////////////////

	
	regress c pol if abs(pol)<=2 & abs(c)<=1, vce(cluster ccode)
	outreg2 using replication_output/table_E_7_cfile_alarm, tex replace
	regress c pol if abs(pol)<=2 & abs(c)<=1 & alarmhi==0, vce(cluster ccode)
	outreg2 using replication_output/table_E_7_cfile_alarm, tex append
	regress c pol if abs(pol)<=2 & abs(c)<=1 & alarmhi==1, vce(cluster ccode)
	outreg2 using replication_output/table_E_7_cfile_alarm, tex append
	
	regress p pol if abs(pol)<=2 & abs(p)<=1, vce(cluster ccode)
	outreg2 using replication_output/table_E_7_pfile_alarm, tex replace
	regress p pol if abs(pol)<=2 & abs(p)<=1 & alarmhi==0, vce(cluster ccode)
	outreg2 using replication_output/table_E_7_pfile_alarm, tex append
	regress p pol if abs(pol)<=2 & abs(p)<=1 & alarmhi==1, vce(cluster ccode)
	outreg2 using replication_output/table_E_7_pfile_alarm, tex append

	////////////////////////////////////////////////////////////////////////////
	//Table F.10: 
	////////////////////////////////////////////////////////////////////////////

	* Logit Models of Interventions with Process-Party Incumbent-Opposition Divisions
	
	gen prefab=(a_gov>0 & b_opp<0) if abs(a_gov)<=1 & abs(b_opp)<=1

	gen prefba=(a_gov<0 & b_opp>0) if abs(a_gov)<=1 & abs(b_opp)<=1

	gen prefnone=(prefab==0 & prefba==0) if abs(a_gov)<=1 & abs(b_opp)<=1

	gen cgov=(c>0) if abs(c)<=1

	gen copp=(c<0) if abs(c)<=1

	gen ppos=(p>0) if abs(p)<=1

	gen pneg=(p<0) if abs(p)<=1
	
	drop if electionid==""

	logit cgov prefab dev v2x_polyarchy_target if abs(a_gov)<=1 & abs(b_opp)<=1 & abs(c)<=1, vce(cluster ccode)
	outreg2 using replication_output/table_F_10_refile, tex replace
	logit copp prefba dev v2x_polyarchy_target if abs(a_gov)<=1 & abs(b_opp)<=1 & abs(c)<=1
	outreg2 using replication_output/table_F_10_refile, tex append
	logit pneg prefab dev v2x_polyarchy_target if abs(a_gov)<=1 & abs(b_opp)<=1 & abs(p)<=1
	outreg2 using replication_output/table_F_10_refile, tex append
	logit ppos prefba dev v2x_polyarchy_target if abs(a_gov)<=1 & abs(b_opp)<=1 & abs(p)<=1
	outreg2 using replication_output/table_F_10_refile, tex append

	////////////////////////////////////////////////////////////////////////////
	//Table 6
	////////////////////////////////////////////////////////////////////////////
	
	* Abs. Change in Relations with US
	
	gen abspol=abs(pol)
	
	logit changeus abspol if v2x_polyarchy_target<.6 & v2x_polyarchy_target!=.
	outreg2 using replication_output/table_6_changeus, tex replace

	logit changeus abspol if v2x_polyarchy_target>=.6 & v2x_polyarchy_target!=.
	outreg2 using replication_output/table_6_changeus, tex append
	
	****************************************************************************
	****************************************************************************	
	** Prepare CMP data
	****************************************************************************	
	****************************************************************************

	gen a_r1=a_rile/50
	gen b_r1=b_rile/50

	gen polrile=a_r1-b_r1
	replace polrile=-2 if polrile<=-2 & polrile!=. 
	replace polrile=2 if polrile>=2 & polrile!=. 
	
	sort electionid i_ccode
	
	////////////////////////////////////////////////////////////////////////////
	// Figure H.9
	////////////////////////////////////////////////////////////////////////////
	
	twoway fpfitci unusalign pol if abs(pol)<=2 & i_ccode==2, estopts(degree(5)) || scatter unusalign pol if abs(pol)<=2 & i_ccode==2, msymb(X) ///
	legend(lab(2 "Polynomial Fit") lab(3 "Actual Values") cols(3)) xtitle("Polarization") ytitle("UNGA Alignment") name(fp1, replace)
	
	twoway fpfitci unusalign a_gov if abs(a_gov)<=1 & i_ccode==2, estopts(degree(5)) || scatter unusalign a_gov if abs(a_gov)<=1 & i_ccode==2, msymb(X) ///
	legend(lab(2 "Polynomial Fit") lab(3 "Actual Values") cols(3)) xtitle("Government Position") ytitle("UNGA Alignment") name(fp2, replace)
	
	grc1leg fp1 fp2
	
	*graph export r2_13_plot_combined.pdf, replace // old name
	graph export replication_output/fig_H_9_unusalign_plot.pdf, replace	
	
	////////////////////////////////////////////////////////////////////////////
	// Figure H.10-H.12
	////////////////////////////////////////////////////////////////////////////

	* Polarization in the Process-Party Data compared to UNGA Voting Alignment

	gsort b_opp a_gov
	
	cap drop group	
	gen group=_n

	* these group values have been labelled so

	* prepare for some graphs

	labmask group, values(cy) // findit labmask, needs package by Cox

	* 6 graphs for all where a_gov and b_opp are between -1 and 1

	* !NEED 20 per cent bigger font

	twoway rspike a_gov b_opp group in 1/36, horizontal ylabel(1(1)36, angle(horizontal) valuelabels) scale(.5) graphregion(color(white)) aspectratio(1.5) xtitle("") ytitle("") xscale(range(-1 1)) xlabel(-1(.5)1) legend(off) || scatter group a_gov in 1/36,  msymbol(X) mcolor(navy) || scatter group b_opp in 1/36, msymbol(Sh) mcolor(navy) || scatter group unusalign in 1/36, mcolor(maroon)

	graph export replication_output/fig_H_10_agovboppunusalign1.pdf, replace

	twoway rspike a_gov b_opp group in 37/72, horizontal ylabel(37/72, angle(horizontal) valuelabels) scale(.5) graphregion(color(white)) aspectratio(1.5) xtitle("") ytitle("") xscale(range(-1 1)) xlabel(-1(.5)1) legend(off) || scatter group a_gov in 37/72,  msymbol(X) mcolor(navy) || scatter group b_opp in 37/72, msymbol(Sh) mcolor(navy) || scatter group unusalign in 37/72, mcolor(maroon)

	graph export replication_output/fig_H_10_agovboppunusalign2.pdf, replace

	twoway rspike a_gov b_opp group in 73/108, horizontal ylabel(72/108, angle(horizontal) valuelabels) scale(.5) graphregion(color(white)) aspectratio(1.5) xtitle("") ytitle("") xscale(range(-1 1)) xlabel(-1(.5)1) legend(off) || scatter group a_gov in 72/108,  msymbol(X) mcolor(navy) || scatter group b_opp in 72/108, msymbol(Sh) mcolor(navy) || scatter group unusalign in 72/108, mcolor(maroon)

	graph export replication_output/fig_H_11_agovboppunusalign3.pdf, replace

	twoway rspike a_gov b_opp group in 109/144, horizontal ylabel(108/144, angle(horizontal) valuelabels) scale(.5) graphregion(color(white)) aspectratio(1.5) xtitle("") ytitle("") xscale(range(-1 1)) xlabel(-1(.5)1) legend(off) || scatter group a_gov in 108/144,  msymbol(X) mcolor(navy) || scatter group b_opp in 108/144, msymbol(Sh) mcolor(navy) || scatter group unusalign in 108/144, mcolor(maroon)

	graph export replication_output/fig_H_11_agovboppunusalign4.pdf, replace

	twoway rspike a_gov b_opp group in 145/180, horizontal ylabel(145/180, angle(horizontal) valuelabels) scale(.5) graphregion(color(white)) aspectratio(1.5) xtitle("") ytitle("") xscale(range(-1 1)) xlabel(-1(.5)1) legend(off) || scatter group a_gov in 145/180,  msymbol(X) mcolor(navy) || scatter group b_opp in 145/180, msymbol(Sh) mcolor(navy) || scatter group unusalign in 145/180, mcolor(maroon)

	graph export replication_output/fig_H_12_agovboppunusalign5.pdf, replace

	twoway rspike a_gov b_opp group in 181/217, horizontal ylabel(181/217, angle(horizontal) valuelabels) scale(.5) graphregion(color(white)) aspectratio(1.5) xtitle("") ytitle("") xscale(range(-1 1)) xlabel(-1(.5)1) legend(off) || scatter group a_gov in 181/217,  msymbol(X) mcolor(navy) || scatter group b_opp in 181/217, msymbol(Sh) mcolor(navy) || scatter group unusalign in 181/217, mcolor(maroon)

	graph export replication_output/fig_H_12_agovboppunusalign6.pdf, replace   

	////////////////////////////////////////////////////////////////////////////
	// Figure I.14
	////////////////////////////////////////////////////////////////////////////
	
	twoway fpfitci polrile pol if abs(polrile)<=2 & abs(pol)<=2 & i_ccode==2, estopts(degree(5)) || scatter polrile pol if abs(polrile)<=2 & abs(pol)<=2 & i_ccode==2, msymb(X) ///
	legend(lab(2 "Polynomial Fit") lab(3 "Actual Values") cols(3)) xtitle("Polarization") ytitle("CMP Polarization") name(fp1cmp,replace) ylab(-2 (1) 1)
	
	twoway fpfitci polrile a_gov if abs(polrile)<=2 & abs(a_gov)<=1 & i_ccode==2, estopts(degree(5)) || scatter polrile a_gov if abs(polrile)<=2 & abs(a_gov)<=1 & i_ccode==2, msymb(X) ///
	legend(lab(2 "Polynomial Fit") lab(3 "Actual Values") cols(3)) xtitle("Government Position") ytitle("CMP Polarization") name(fp2cmp,replace) ylab(-2 (1) 1)
	
	grc1leg fp1cmp fp2cmp
	
	*graph export cmp_plot_combined.pdf, replace // old name
	graph export replication_output/fig_I_14_cmp_plot.pdf, replace

	
	////////////////////////////////////////////////////////////////////////////
	// Figure I.15
	////////////////////////////////////////////////////////////////////////////
	
	preserve
	
	* Polarization Measurement Based on the CMP

	keep if CMP_merge==3

	sort b_r1

	cap drop group

	gen group=_n

	* these group values have been labelled so

	labmask group, values(cy)

	twoway rspike a_r1 b_r1 group in 1/28, horizontal ylabel(1(1)28, angle(horizontal) valuelabels) scale(.5) graphregion(color(white)) aspectratio(1.5) xtitle("") ytitle("") xscale(range(-1 1)) xlabel(-1(.5)1) legend(off) lpattern(dash) || scatter group a_r1 in 1/28,  msymbol(X) mcolor(navy) || scatter group b_r1 in 1/28, msymbol(Sh) mcolor(navy)

	graph export replication_output/fig_I_15_abrile1.pdf, replace

	twoway rspike a_r1 b_r1 group in 29/56, horizontal ylabel(29/56, angle(horizontal) valuelabels) scale(.5) graphregion(color(white)) aspectratio(1.5) xtitle("") ytitle("") xscale(range(-1 1)) xlabel(-1(.5)1) legend(off) lpattern(dash) || scatter group a_r1 in 29/56,  msymbol(X) mcolor(navy) || scatter group b_r1 in 29/56, msymbol(Sh) mcolor(navy)

	graph export replication_output/fig_I_15_abrile2.pdf, replace
	
	restore
	
	////////////////////////////////////////////////////////////////////////////
	// Table 3 (Summary Table)
	////////////////////////////////////////////////////////////////////////////	
		
	preserve
	label variable c "Candidate Interventions"
	label variable p "Process Interventions"
	label variable pol "Polarization (Process Party)"
	label variable dev "Developed Country"
	label variable v2x_polyarchy_target "V-Dem Polyarchy"
	label variable unusalign "UNGA Voting Alignment"
	label variable polrile "Left-Right (CMP)"
	
	replace c = . if abs(c) > 1
	replace p = . if abs(p) > 1
	replace pol = . if abs(pol)>2

	keep if i_ccode==2 // & abs(pol)<=2 & ( abs(c)<=1 | abs(p)<=1 )
	*outreg2 using summary_table.tex, replace sum(log) keep(c p pol dev v2x_polyarchy_target unusalign polrile) tex(frag) label
	outreg2 using replication_output/table_3_summary_table.tex, replace sum(log) keep(c p pol dev v2x_polyarchy_target unusalign) tex(frag) label	
	restore
	
	////////////////////////////////////////////////////////////////////////////
	// Figure 2 
	////////////////////////////////////////////////////////////////////////////

	* US Process and Candidate Interventions: OLS on Process-Party data, UNGA voting alignment, L-R polarization from CMP
	
	twoway (lfitci c pol if abs(pol)<=2 & abs(c)<=1, ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitpp, replace))  || (lfitci p2 pol if abs(pol)<=2 & abs(p)<=1, ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))
	twoway (lfitci c unusalign if abs(c)<=1, ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitun, replace)) || (lfitci p2 unusalign if abs(pol)<=2, ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))
	twoway (lfitci c polrile if abs(polrile)<=2 & abs(c)<=1, ciplot(rarea) lcolor(red) alcolor(gs12) range(-2 2) n(5) xlabel(-2(1)2, labsize(small)) name(lfitrl, replace)) || (lfitci p2 polrile if abs(polrile)<=2 & abs(p)<=1, ciplot(rarea) lcolor(blue) alcolor(gs12) range(-2 2) n(5) yscale(range(-1 3.2)) graphregion(color(white)) legend(off) xtitle("") ylabel(-1 "g-" 0 "0" 1 "g+" 2 "0" 3 "d+", nogrid labsize(small) angle(horizontal)) yline(0 2, lcolor(gs15)))
	
	gr combine lfitpp lfitun lfitrl, col(3) iscale(1) graphregion(color(white))

	graph export replication_output/fig_2_lfitcomb.pdf, replace
	
	////////////////////////////////////////////////////////////////////////////
	// Figure 2 & Table 2
	////////////////////////////////////////////////////////////////////////////
	
	regress c pol if abs(pol)<=2 & abs(c)<=1, vce(cluster ccode)
	outreg2 using replication_output/table_2_cfile, tex replace
	
	regress p pol if abs(pol)<=2 & abs(p)<=1, vce(cluster ccode)
	outreg2 using replication_output/table_2_pfile, tex replace
	
	regress c polrile if abs(polrile)<=2 & abs(c)<=1, vce(cluster ccode)
	outreg2 using replication_output/table_2_cfile, tex append

	regress p polrile if abs(polrile)<=2 & abs(p)<=1, vce(cluster ccode)
	outreg2 using replication_output/table_2_pfile, tex append
	
	regress c unusalign if abs(c)<=1, vce(cluster ccode)
	outreg2 using replication_output/table_2_cfile, tex append

	regress p unusalign if abs(p)<=1, vce(cluster ccode)
	outreg2 using replication_output/table_2_pfile, tex append
	
	////////////////////////////////////////////////////////////////////////////
	// Table 4 (Logit Regressions Table)
	////////////////////////////////////////////////////////////////////////////	
	
	label variable c "Candidate Interventions"
	label variable p "Process Interventions"
	label variable pol "Polarization (Process Party)"
	label variable dev "Developed Country"
	label variable v2x_polyarchy_target "V-Dem Polyarchy"
	label variable unusalign "UNGA Voting Alignment"
	label variable polrile "Left-Right (CMP)"
	
	gen post_cw = (year>1990)
	label variabl post_cw "Post Cold War"
	
	ologit c pol dev v2x_polyarchy_target post_cw  if i_ccode==2 & abs(pol)<=2 & abs(c)<=1 , vce(cluster ccode)
	*outreg2 using logit_polarization_noregion, tex replace label
	outreg2 using replication_output/table_4_logit_polarization, tex replace label

	logit cgov  pol dev v2x_polyarchy_target post_cw  if i_ccode==2 & abs(pol)<=2 & abs(c)<=1 , vce(cluster ccode)
	*outreg2 using logit_polarization_noregion, tex append label
	outreg2 using replication_output/table_4_logit_polarization, tex append label
	
	logit copp  pol dev v2x_polyarchy_target post_cw  if i_ccode==2 & abs(pol)<=2 & abs(c)<=1 , vce(cluster ccode)
	*outreg2 using logit_polarization_noregion, tex append label
	outreg2 using replication_output/table_4_logit_polarization, tex append label
	
	ologit p pol dev v2x_polyarchy_target post_cw  if i_ccode==2 & abs(pol)<=2 & abs(p)<=1 , vce(cluster ccode)
	*outreg2 using logit_polarization_noregion, tex append label
	outreg2 using replication_output/table_4_logit_polarization, tex append label
	
	logit ppos  pol dev v2x_polyarchy_target post_cw  if i_ccode==2 & abs(pol)<=2 & abs(p)<=1 , vce(cluster ccode)
	*outreg2 using logit_polarization_noregion, tex append label
	outreg2 using replication_output/table_4_logit_polarization, tex append label
	
	logit pneg  pol dev v2x_polyarchy_target post_cw  if i_ccode==2 & abs(pol)<=2 & abs(p)<=1 , vce(cluster ccode)
	*outreg2 using logit_polarization_noregion, tex append label
	outreg2 using replication_output/table_4_logit_polarization, tex append label
	
	////////////////////////////////////////////////////////////////////////////
	// Table F 8 (SUR)
	////////////////////////////////////////////////////////////////////////////	
	
	cap net from http://www.stata-press.com/data/ml4/
	cap net install ml4_ado
	cap ml4_ado
	
	svyset ccode
	svy: mysureg (c pol) (p pol)
	*outreg2 using sur_output , tex replace
	outreg2 using replication_output/table_F_8_sur_output , tex replace
	
	svy: mysureg (c unusalign) (p unusalign) if abs(c)<=1 & abs(p)<=1
	*outreg2 using sur_output , tex append
	outreg2 using replication_output/table_F_8_sur_output , tex append
	
	svy: mysureg (c polrile) (p polrile)
	*outreg2 using sur_output , tex append
	outreg2 using replication_output/table_F_8_sur_output , tex append

	////////////////////////////////////////////////////////////////////////////
	// Table F.9 (Logit Regressions Table w/ region controls)
	////////////////////////////////////////////////////////////////////////////		

	ologit c pol dev v2x_polyarchy_target post_cw i.region_code if i_ccode==2 & abs(pol)<=2 & abs(c)<=1 , vce(cluster ccode)
	*outreg2 using logit_polarization, tex replace label
	outreg2 using replication_output/table_F_9_logit_polarization_w_region, tex replace label
	
	logit cgov  pol dev v2x_polyarchy_target post_cw i.region_code if i_ccode==2 & abs(pol)<=2 & abs(c)<=1 , vce(cluster ccode)
	*outreg2 using logit_polarization, tex append label
	outreg2 using replication_output/table_F_9_logit_polarization_w_region, tex append label
	
	logit copp  pol dev v2x_polyarchy_target post_cw i.region_code if i_ccode==2 & abs(pol)<=2 & abs(c)<=1 , vce(cluster ccode)
	*outreg2 using logit_polarization, tex append label
	outreg2 using replication_output/table_F_9_logit_polarization_w_region, tex append label

	
	ologit p pol dev v2x_polyarchy_target post_cw i.region_code if i_ccode==2 & abs(pol)<=2 & abs(p)<=1 , vce(cluster ccode)
	*outreg2 using logit_polarization, tex append label
	outreg2 using replication_output/table_F_9_logit_polarization_w_region, tex append label

	logit ppos  pol dev v2x_polyarchy_target post_cw i.region_code if i_ccode==2 & abs(pol)<=2 & abs(p)<=1 , vce(cluster ccode)
	*outreg2 using logit_polarization, tex append label
	outreg2 using replication_output/table_F_9_logit_polarization_w_region, tex append label
	
	logit pneg  pol dev v2x_polyarchy_target post_cw i.region_code if i_ccode==2 & abs(pol)<=2 & abs(p)<=1 , vce(cluster ccode)
	*outreg2 using logit_polarization, tex append label
	outreg2 using replication_output/table_F_9_logit_polarization_w_region, tex append label


	////////////////////////////////////////////////////////////////////////////
	// Table G.11
	////////////////////////////////////////////////////////////////////////////

	* Criticism of Elections in US Congress – Different Incumbent-Opposition Divisions

	* HRDOC

	regress  hrdocyr pol if abs(pol)<=2, vce(cluster ccode)
	outreg2 using replication_output/table_G_11_hrdocyr, tex replace
	regress hrdocyr unusalign, vce(cluster ccode)
	outreg2 using replication_output/table_G_11_hrdocyr, tex append
	regress hrdocyr polrile if abs(polrile)<=2, vce(cluster ccode)
	outreg2 using replication_output/table_G_11_hrdocyr, tex append
	
	////////////////////////////////////////////////////////////////////////////
	// Figure 1
	////////////////////////////////////////////////////////////////////////////
	
	drop _all

	import delimited topic-classification.csv, numericc(43 44 45 46 47 48 49) 

	gen econ_p=.
	gen armconfl_p=.
	gen align_p=.
	gen demhr_p=.
	gen left_p=.
	gen globalbads_p=.

	egen max1=rowmax(econ armconfl align demhr left globalbads)

	forval x = 1/`=_N' { 

		foreach i of varlist econ armconfl align demhr left globalbads {

			if `i'[`x']==max1[`x'] & `i'[`x']!=. {
				replace `i'_p=1 in `x'
				replace `i'=. in `x'
			}
		}

	}

	egen max2=rowmax(econ armconfl align demhr left globalbads)

	forval x = 1/`=_N' { 


		foreach i of varlist econ armconfl align demhr left globalbads {

			if `i'[`x']==max2[`x'] & `i'[`x']!=. {
				replace `i'_p=2 in `x'
				replace `i'=. in `x'

			}
		}

	}	


	egen max3=rowmax(econ armconfl align demhr left globalbads)

	forval x = 1/`=_N' { 


		foreach i of varlist econ armconfl align demhr left globalbads {

			if `i'[`x']==max3[`x'] & `i'[`x']!=. {
				replace `i'_p=3 in `x'
				replace `i'=. in `x'

			}
		}

	}	

	egen max4=rowmax(econ armconfl align demhr left globalbads)

	forval x = 1/`=_N' { 


		foreach i of varlist econ armconfl align demhr left globalbads {

			if `i'[`x']==max4[`x'] & `i'[`x']!=. {
				replace `i'_p=4 in `x'
				replace `i'=. in `x'

			}
		}

	}	


	egen max5=rowmax(econ armconfl align demhr left globalbads)

	forval x = 1/`=_N' { 


		foreach i of varlist econ armconfl align demhr left globalbads {

			if `i'[`x']==max5[`x'] & `i'[`x']!=. {
				replace `i'_p=5 in `x'
				replace `i'=. in `x'

			}
		}

	}	


	egen max6=rowmax(econ armconfl align demhr left globalbads)

	forval x = 1/`=_N' { 


		foreach i of varlist econ armconfl align demhr left globalbads {

			if `i'[`x']==max6[`x'] & `i'[`x']!=. {
				replace `i'_p=6 in `x'
				replace `i'=. in `x'

			}
		}

	}	


	drop max*

	twoway (hist align_p, freq lcolor(gs8) fcolor(gs8)) (hist armconfl_p, freq lcolor(navy) fcolor(none) lwidth(thin)), legend(off) xtitle("Align [Conflict]") graphregion(color(white))  ytitle("") ylabel(0(100)600,nogrid) name(issues12, replace) yscale(range(1 650) lcolor(white)) 

	twoway (hist econ_p, freq fcolor(gs10) lcolor(gs10)) (hist demhr_p, freq fcolor(none) lcolor(red) lwidth(thin)), legend(off) xtitle("Economic [Dem & HR]") graphregion(color(white))  ytitle("") ylabel("",nogrid) name(issues34, replace) yscale(range(1 650) lcolor(white))

	twoway (hist left_p, freq fcolor(gs12) lcolor(gs12)) (hist globalbads_p, freq fcolor(none) lcolor(green) lwidth(thin)), legend(off) xtitle("Left-Right [Bads]") graphregion(color(white))  ytitle("") ylabel("",nogrid) name(issues56, replace) yscale(range(1 650) lcolor(white))

	gr combine issues12 issues34 issues56, col(3) iscale(1) graphregion(color(white))

	graph export replication_output/fig_1_allissues.pdf, replace

	****************************************************************************
	****************************************************************************	
	** Clean up a few things
	****************************************************************************	
	****************************************************************************
	
	graph close _all
	
	//Clean up after package
	foreach filetype in "my*.hlp" "my*.ado" "ml*.hlp" {
	di "Erasing all files of filetype `filetype'"
	local files: dir "$path" files "`filetype'"
	foreach file in `files' {
	  di "Will erase `file'"
	  quietly cd $path
	  erase `file'
	}
	}
